home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1996 April: Mac OS SDK / Dev.CD Apr 96 SDK / Dev.CD Apr 96 SDK1.toast / Development Kits (Disc 1) / OpenDoc / Sample Code / Scripting / OpenDoc demo script.txt < prev    next >
Encoding:
Text File  |  1995-11-09  |  9.6 KB  |  270 lines  |  [TEXT/ToyS]

  1. -- change this if you don't want to see every property every time
  2. property displayresult : true
  3.  
  4. (* These lists contain the names of editors that might show up in a document to be
  5.  * targeted by this script.  If you're adding new part editors to the possible
  6.  * set, include their names here if appropriate.
  7.  *)
  8.  
  9. property gScriptableParts : {"Sound Editor 1.0"}
  10.  
  11. (* There's a bug in one sample part such that it returns null when
  12.  asked to create an embedded frames iterator rather than throwing an
  13.  error (and perhaps in OpenDoc since it doesn't check for null...)  If
  14.  you include this part or another with this bug in a document targeted
  15.  by this script, be sure to list it here or this script will crash OpenDoc. *)
  16. property gKnownDangerousLeaves : {"Panel Editor 1.0"}
  17.  
  18. on SimpleDisplay(str)
  19.     display dialog str buttons {"OK"} default button "OK"
  20. end SimpleDisplay
  21.  
  22. on ConstantToString(const)
  23.     if const = bold then
  24.         return "bold"
  25.     else if const = plain then
  26.         return "plain"
  27.     else if const = italic then
  28.         return "italic"
  29.     else if const = underline then
  30.         return "underline"
  31.     else if const = outline then
  32.         return "outline"
  33.     else if const = shadow then
  34.         return "shadow"
  35.     else
  36.         return "unknown constant"
  37.     end if
  38. end ConstantToString
  39.  
  40. on IfDisplay(result, comment, buttonText)
  41.     if displayresult then
  42.         set needsQuote to true
  43.         if (class of result) = boolean then
  44.             if result then
  45.                 set result to "true"
  46.             else
  47.                 set result to "false"
  48.             end if
  49.         else if (class of result) = constant then
  50.             set result to my ConstantToString(result)
  51.         else if (class of result) ≠ string then
  52.             set needsQuote to false
  53.         end if
  54.         set displayString to comment
  55.         if needsQuote then
  56.             set displayString to displayString & "\""
  57.         end if
  58.         set displayString to displayString & (result)
  59.         if needsQuote then
  60.             set displayString to displayString & "\""
  61.         end if
  62.         display dialog displayString buttons {buttonText} default button buttonText
  63.     end if
  64. end IfDisplay
  65.  
  66. (****************************************************************
  67. * Get and display every property accessible through the default
  68. * accessors/handlers.  Some of these are read/write though we
  69. * don't demonstrate that here.
  70. ****************************************************************)
  71. on InfoForPart(partID)
  72.     if partID ≠ 0 then
  73.         tell application "Your Sample Document"
  74.             tell part id partID
  75.                 my IfDisplay(name, "name of part is ", "Keen")
  76.                 -- sometimes "comment" doesn't work....
  77.                 my IfDisplay(comment, "comment of part is ", "Wow!")
  78.                 --        my IfDisplay(its category, "category of root part is ")
  79.                 my IfDisplay(part size, "part size of part is ", "Neato")
  80.                 my IfDisplay(creation date, "creation date of part is ", "Fabulous")
  81.                 my IfDisplay(modification date, "modification date of part is ", "Hot damn")
  82.                 my IfDisplay(author, "author of part is ", "Special")
  83.                 my IfDisplay(bundled, "bundled property of part is ", "Way cool, Dude")
  84.                 my IfDisplay(show links, "show links of part is ", "I'm so happy for you!")
  85.                 my IfDisplay(editor, "editor name of part is ", "Really?")
  86.                 my IfDisplay(id, "ID of part is ", "Do tell")
  87.                 my IfDisplay(stationery, "stationery property of part is ", "Na und?")
  88.                 
  89.             end tell
  90.         end tell
  91.     end if
  92. end InfoForPart
  93.  
  94. (****************************************************************
  95. * Starting with the ID passed in, build a flat list of that ID
  96. * and the IDs of all parts contained within that one.  There's no
  97. * particular order to the IDs.
  98. * The TRY statement deals with parts that don't have any embedded
  99. * parts, which will often signal this by returning an error
  100. * rather than an empty list  -- as do the Default Semantic
  101. * Interfaces's accessors.
  102. ****************************************************************)
  103. on BuildFullList(startID)
  104.     tell application "Your Sample Document"
  105.         set curlist to startID as list
  106.         set finallist to {}
  107.         set newlist to {1} -- will be discarded
  108.         repeat until newlist = {}
  109.             set newlist to {}
  110.             repeat with oneID in curlist
  111.                 if editor of part id oneID is not in gKnownDangerousLeaves then
  112.                     try
  113.                         -- throw in a whose clause just to show that they work :-)
  114.                         set newlist to newlist & id of (every part whose name ≠ "improbable__name") of part id oneID
  115.                     on error
  116.                     end try
  117.                 end if
  118.             end repeat
  119.             set finallist to finallist & curlist
  120.             set curlist to newlist
  121.         end repeat
  122.     end tell
  123.     return finallist & curlist
  124. end BuildFullList
  125.  
  126. (****************************************************************
  127. * create a list of editors in the document.  Each entry is a list whose first element
  128. * is the name of the editor, and whose second is a list of IDs of parts bound to
  129. * the named editor.
  130. ****************************************************************)
  131. on RecordInfoOnPart(edtr, partID, partInfoList)
  132.     set tmplist to contents of partInfoList
  133.     repeat with entry in tmplist
  134.         if edtr = item 1 of entry then
  135.             set sublist to item 2 of entry
  136.             set item 2 of entry to sublist & {partID}
  137.             return
  138.         end if
  139.     end repeat
  140.     -- if we get here there's no entry
  141.     set newlist to {edtr, {partID}}
  142.     set contents of partInfoList to tmplist & {newlist}
  143. end RecordInfoOnPart
  144.  
  145. (****************************************************************
  146. * Go through the editors record, displaying each editor's name
  147. * and the number of parts it has in the document.
  148. ****************************************************************)
  149. on DisplayPartInfoRecord(infoList)
  150.     set str to "Editors represented and by how many parts:" & return
  151.     repeat with entry in infoList
  152.         set numEditors to length of item 2 of entry
  153.         set str to str & item 1 of entry & ": " & (numEditors as text) & ";" & return
  154.     end repeat
  155.     my SimpleDisplay(str)
  156. end DisplayPartInfoRecord
  157.  
  158. (****************************************************************
  159. * GetCurrentDate: for some reason, Sound Editor eats the event
  160. * intended for the current date osax, so we have to move it outside
  161. * its scope.
  162. ****************************************************************)
  163. on GetCurrentDate()
  164.     return current date
  165. end GetCurrentDate
  166.  
  167. (****************************************************************
  168. * For each editor, try to make it play (with the expectation that only
  169. * Sound Editor will do so).  The TRY statement here assumes that if
  170. * one part fails it's because the editor doesn't understand playing
  171. * -- so we don't bother trying to play any remaining parts
  172. * with that editor.
  173. ****************************************************************)
  174. on TryToPlay(partInfoList)
  175.     tell application "Your Sample Document"
  176.         repeat with partrecord in partInfoList
  177.             repeat with partID in item 2 of partrecord
  178.                 try
  179.                     tell part id partID
  180.                         play
  181.                         set soundLength to sound length
  182.                         set stopTime to (my GetCurrentDate()) + soundLength
  183.                         repeat until my GetCurrentDate() ≥ stopTime
  184.                         end repeat
  185.                         stop
  186.                     end tell
  187.                 on error
  188.                     my SimpleDisplay("Editor \"" & editor of part id partID & "\" won't play with me.")
  189.                     set comment of part id partID to "I'm not a player."
  190.                     exit repeat -- we know the part can't handle it now, so bail on the rest of the list
  191.                 end try
  192.             end repeat
  193.         end repeat
  194.     end tell
  195. end TryToPlay
  196.  
  197. (****************************************************************
  198. * Given the editors record, return the ID of a part with a given
  199. * editor.  Right now we just return the first one.
  200. ****************************************************************)
  201. on GetPartThatMatches(partNameList, partInfoList, matchSought)
  202.     repeat with partrecord in partInfoList
  203.         if matchSought and item 1 of partrecord is in partNameList then
  204.             return item 1 of item 2 of partrecord
  205.         else if not matchSought and item 1 of partrecord is not in partNameList then
  206.             return item 1 of item 2 of partrecord
  207.         end if
  208.     end repeat
  209.     return 0
  210. end GetPartThatMatches
  211.  
  212. (****************************************************************
  213. * Given a list of IDs of parts in the document, find the one
  214. * most deeply nested and return.  We could do this without
  215. * the container property, but the whole point is to show it off.
  216. ****************************************************************)
  217. on FindMaxDepth(partIDList)
  218.     set maxSoFar to 0
  219.     tell application "Your Sample Document"
  220.         set rootID to id
  221.         repeat with oneID in partIDList
  222.             set thisMax to 0
  223.             set thisParent to contents of oneID
  224.             repeat until thisParent = rootID
  225.                 set thisParent to id of (get container of part id thisParent)
  226.                 set thisMax to thisMax + 1
  227.             end repeat
  228.             if thisMax > maxSoFar then set maxSoFar to thisMax
  229.         end repeat
  230.     end tell
  231.     return maxSoFar
  232. end FindMaxDepth
  233.  
  234. -- HERE IS WHERE "MAIN" BEGINS....
  235.  
  236. tell application "Your Sample Document"
  237.     set comments to "I'm the root part"
  238.     
  239.     set listOfAllPartIDs to my BuildFullList(id)
  240.     
  241.     set partInfoList to {}
  242.     repeat with partID in listOfAllPartIDs
  243.         set edtr to editor of part id partID
  244.         my RecordInfoOnPart(edtr, contents of partID, a reference to partInfoList)
  245.     end repeat
  246.     
  247.     set aPartID to my GetPartThatMatches(gScriptableParts, partInfoList, true)
  248.     if aPartID ≠ 0 then
  249.         my SimpleDisplay("Now we'll show the info properties for a scriptable part")
  250.         my InfoForPart(aPartID)
  251.     else
  252.         my SimpleDisplay("Couldn't find a scriptable part: did you include one we don't know about?")
  253.     end if
  254.     
  255.     set aPartID to my GetPartThatMatches(gScriptableParts, partInfoList, false)
  256.     if aPartID ≠ 0 then
  257.         my SimpleDisplay("Now we'll show the info properties for a non-scriptable part")
  258.         my InfoForPart(aPartID)
  259.     else
  260.         my SimpleDisplay("Couldn't find a non-scriptable part: did you include one we don't know about?")
  261.     end if
  262.     
  263.     my DisplayPartInfoRecord(partInfoList)
  264.     my SimpleDisplay("now I'll play all the parts that'll let me")
  265.     my TryToPlay(partInfoList)
  266.     
  267.     my SimpleDisplay("The greatest depth of nesting in this document is " & my FindMaxDepth(listOfAllPartIDs) & ".")
  268.     
  269.     my SimpleDisplay("END OF DEMO")
  270. end tell